<?php
namespace app\common\server;
use x\Xbase;
class UserUpgrade extends Xbase
{


    /**
     * 余额支付
     * @param  [type] $order_no [description]
     * @return [type]           [description]
     */
    public function pay($order_no){
        //查询商品订单
        $map = [];
        $map[] = ["and", "order_no", "=", $order_no];
        $map[] = ["and", "pay_status", "=", 0];
        $w = where($map);
        $order_info = $this->db->query("select * from user_upgrade $w[0]", $w[1],1);
        if (empty($order_info)) {
            return $this->setError('订单不存在!');
        }
        $userinfo = $this->db->query('select * from user where id = ?',[$order_info['user_id']],1);

        $this->db->begin();
        $mysql = [];
        //修改订单支付状态
        $d = ["pay_status" => 1,'pay_time'=>time()];
        $map = ['order_no=?', [$order_no]];
        $mysql[] = $this->db->update("user_upgrade", $d, $map);
        #更新订单记录
        $data = [
            'level'=>$order_info["level"],
            'vip_nums'=>$userinfo["vip_nums"]+$order_info["stock"],
            'vip_sum_nums'=>$userinfo["vip_sum_nums"]+$order_info["stock"],
        ];
        $mysql[] = $this->db->update("user", $data, ['id=?', [$order_info['user_id']]]);
        //计算各种奖项

        if($order_info["level"] == 1){
            //添加虚拟店铺
            $shop = [
                "user_id" => $userinfo['id'],
                "username" => $userinfo['username'],
                "phone" => $userinfo['phone'],
                "create_time" => time(),
            ];
            $mysql[] = $this->db->insert("user_shop",$shop);
        }
        $mysql[] = m('Userbill', 'xcom')->doit($order_no, $userinfo['id'], 203, 'user_upgrade', $order_info['id'], $order_info['money'],'money','消费', 0);
        $rs = $this->db->end($mysql);
        if (!$rs) {
            return $this->setError('支付失败!');
        }
        return true;
    }

}
